﻿using Farming.EntityFarmworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Farming.Repositories
{
    public class Repository<T> : IRepository<T> where T : class, new()
    {
        private readonly FarmingDbContext db;
        public Repository(FarmingDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取泛型集合
        /// </summary>
        /// <returns></returns>
        public virtual async Task<IList<T>> GetAllAsync()
        {
            return await db.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> GetValueAsync(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().Where(expression).ToListAsync();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<int> AddAsync(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public virtual async Task<int> AddRangeAsync(List<T> entities)
        {
            await db.Set<T>().AddRangeAsync(entities);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<int> DelAsync(int Id)
        {
            var n = await db.Set<T>().FindAsync(Id);
            db.Set<T>().Remove(n);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件批量删除
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public virtual async Task<int> BatchDelAsync(Expression<Func<T, bool>> expression)
        {
            var res = await db.Set<T>().Where(expression).ToListAsync();
            db.Set<T>().RemoveRange(res);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> GetByIdAsync(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 查询单条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> GetByIdAsync(Expression<Func<T,bool>> expression)
        {
            return await db.Set<T>().Where(expression).FirstOrDefaultAsync();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<int> UpdAsync(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        public virtual async Task<int> BatchUpdAsync(List<T> entities)
        {
            db.Set<T>().UpdateRange(entities);
            return await db.SaveChangesAsync();
        }


    }
}
